/*
 * led.h
 *
 *  Created on: 03.11.2011
 *      Author: g.kruglov
 */

#ifndef LED_H_
#define LED_H_

#include "stm32l1xx.h"
#include "stm32l1xx_tim.h"
#include "kl_lib_l15x.h"

class Led_t {
protected:
    uint16_t IPinNumber;
    GPIO_TypeDef* IGPIO;
public:
    void Init(GPIO_TypeDef *PGpioPort, uint16_t APinNumber) {
        IGPIO = PGpioPort;
        IPinNumber = APinNumber;
        PinSetupOut(PGpioPort, APinNumber, omPushPull, pudPullDown, ps10MHz);
    }
    void On(void)  { PinSet  (IGPIO, IPinNumber); }
    void Off(void) { PinClear(IGPIO, IPinNumber); }
};

class LedBlink_t : public Led_t {
private:
    uint32_t ITimer, IBlinkDelay;
    bool IsInsideBlink;
public:
    void Disable(void) { Off(); IsInsideBlink = false; }
    void Blink(uint32_t ABlinkDelay);
    void Task(void);
};


// Adjust delay depending on value
static inline uint32_t SetupSmoothDelay(uint16_t AValue) {
    return (uint32_t)(700 / (AValue+10));
}

// Smooth on & off.
class LedSmooth_t {
private:
    uint32_t IDelayTmr, IDelay;
    TIM_TypeDef* IPwmTimer;
    uint16_t *ICCR, IValue, INeededValue;
public:
    uint16_t IWasValue;
    void Init(GPIO_TypeDef *AGpio, uint16_t APinNumber, TIM_TypeDef* ATimer, uint16_t ATopValue, uint16_t APrescaler, uint8_t AChannelNumber, bool InvertedPolarity);
    void Task(void);
    void IllumeTask(void);
    void Set(uint16_t AValue)   { *ICCR = AValue; IValue = AValue; }
    void SetSmoothly(uint16_t AValue) { INeededValue = AValue; }
    uint16_t GetValue(void)     { return *ICCR; }
};


#endif /* LED_H_ */
